﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CNotebook;
using CNotebook.Sales;

namespace NotebookS2.Sales.Logs
{
    public partial class LOGS_SalesInvoices : Form
    {
        public LOGS_SalesInvoices()
        {
            InitializeComponent();
        }

        public string ConnectionString { get; set; }

        public void PrintPreview()
        {
            gridControl1.ShowPrintPreview();
        }

        void BindGrid()
        {
            InvoiceManager oManager = new InvoiceManager();
            DataTable dt = new DataTable();

            // Retreiving Data
            oManager.ConnectionString = this.ConnectionString;            
            oManager.Open();
            dt = oManager.GetInvoiceLogsByDocument(Convert.ToDateTime(dtFROM.EditValue), Convert.ToDateTime(dtTO.EditValue), chkDraft.Checked, chkPosted.Checked,
                                                         chkOpen.Checked, chkClosed.Checked);
            oManager.Close();

            // Setting the data to Grid
            gridControl1.DataSource = dt;
            gridControl1.Refresh();
            
        }

        void BindGridPerItem()
        {
            InvoiceManager oManager = new InvoiceManager();
            DataTable dt = new DataTable();

            // Retreiving Data
            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();
            dt = oManager.GetInvoiceLogsByItems(Convert.ToDateTime(dtFROMItems.EditValue), Convert.ToDateTime(dtTOItems.EditValue), chkDraftPerItem.Checked, chkPostedPerItem.Checked,
                                                         chkOpenPerItem.Checked, chkClosedPerItem.Checked);
            oManager.Close();

            // Setting the data to Grid
            gridControlPerItem.DataSource = dt;
            gridControlPerItem.Refresh();

        }

        void BindGridPerDoc()
        {
            InvoiceManager oManager = new InvoiceManager();
            DataTable dt = new DataTable();

            // Retreiving Data
            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();
            dt = oManager.GetInvoiceLogsByItems(Convert.ToDateTime(dtFROMItems.EditValue), Convert.ToDateTime(dtTOItems.EditValue), chkDraft.Checked, chkPosted.Checked,
                                                         chkOpen.Checked, chkClosed.Checked);
            oManager.Close();

            // Setting the data to Grid
            gridControl1.DataSource = dt;
            gridControl1.Refresh();

        }

        private void btnPreview_Click(object sender, EventArgs e)
        {
            DevExpress.XtraSplashScreen.SplashScreenManager.ShowForm(typeof(WaitForm2));
            BindGrid();
            DevExpress.XtraSplashScreen.SplashScreenManager.CloseForm();
        }

        private void LOGS_Invoices_by_Document_Load(object sender, EventArgs e)
        {
            dtFROM.EditValue = System.DateTime.Now;
            dtTO.EditValue = System.DateTime.Now;

            dtFROMItems.EditValue = System.DateTime.Now;
            dtTOItems.EditValue = System.DateTime.Now;
        }

        private void btnRetrieveItems_Click(object sender, EventArgs e)
        {
            DevExpress.XtraSplashScreen.SplashScreenManager.ShowForm(typeof(WaitForm2));
            BindGridPerItem();
            DevExpress.XtraSplashScreen.SplashScreenManager.CloseForm();
        }
    }
}
